/*
 * @(#)FormTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.struts.util.ResponseUtils;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.Utility;
/**
  FormTag.java

  <p>This class is to extend the FormTag provided by Apache Struts. This class
     is able to create hidden fields for trace the change during input. </p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;org.apache.struts.taglib.html.FormTag&lt;/tagclass&gt;
     [TO]
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.FormTag&lt;/tagclass&gt;

     [ADDED]
     &lt;attribute&gt;
     &lt;name&gt;traceChange&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;disabled&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;textOnly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
  </pre>
  </p>

    @author          Zoe Shum
    @company         DCIVision Limited
    @creation date   09/07/2003
    @version         $Revision: 1.20.2.2 $
*/

public class FormTag extends org.apache.struts.taglib.html.FormTag {

  public static final String REVISION = "$Revision: 1.20.2.2 $";


  private final String IS_FORM_MODIFIED = "isFormModified";

  //default enable trace change
  protected boolean traceChange = false;
  protected boolean disabled = false;

  /**
   * textOnly actually means "Read Only form", this attribute would be propagated to other tag for processing.
   */
  protected boolean textOnly = false;

  /**
   * Render the beginning of this form.
   *
   * @exception JspException if a JSP exception has occurred
   */
  public int doStartTag() throws JspException {
    String originalOnsubmit = super.getOnsubmit();
    originalOnsubmit = (originalOnsubmit==null) ? "return(false);" : (originalOnsubmit + ";return(false);");
    super.setOnsubmit(originalOnsubmit);

    if (traceChange) {
      originalOnsubmit = (originalOnsubmit==null) ? "" : originalOnsubmit;
      super.setOnsubmit(BaseHandler.ONSUBMIT_JS_FUNCTION + originalOnsubmit);
    }

    super.doStartTag();

    if (traceChange) {
      StringBuffer results = new StringBuffer();
      results.append("\n<input type=\"hidden\" name=\"");
      results.append(IS_FORM_MODIFIED);
      results.append("\" value=\"false\">");
      ResponseUtils.write(pageContext, results.toString());
    }

    //Set the Action Type into request
    this.setActionMode();

    // Continue processing this page
    return (EVAL_BODY_INCLUDE);
  }

  public int doEndTag() throws JspException {
    String showHeaderFooter = (String) pageContext.getRequest().getAttribute("SHOW_HEADER_FOOTER");
    if (!Utility.isEmpty(showHeaderFooter)){
      StringBuffer buffer = new StringBuffer();
      buffer.append("<input type=\"hidden\" name=\"SHOW_HEADER_FOOTER\" value=\""+showHeaderFooter+"\"/>");
      try{
          ResponseUtils.write(pageContext, buffer.toString());
      }catch(Exception e){
          ResponseUtils.write(pageContext, "Error in writing hidden value:"+e.getMessage());
      }
    }
    return(super.doEndTag());

   }

  public void setTraceChange(boolean val){
    traceChange=val;
  }

  public boolean getTraceChange(){
    return traceChange;
  }

  public void setDisabled(boolean val){
    disabled=val;
  }

  public boolean getDisabled(){
    return disabled;
  }

  public void setTextOnly(boolean val){
    textOnly=val;
  }

  public boolean getTextOnly(){
    return textOnly;
  }

  /**
   *  Method getActionType() - get the correct action URL and String name
   *
   *  @param      request                    "HttpServletRequest" of page
   *  @param      sysFunction                "SysFunction" bean of current form function
   *  @param      formBeanName               "String" of current used bean name, used ot identify the correct action of current form function
   *  @return     void
   *  @throws     No exception throws
   */
  protected void setActionMode() {

    String actionMode = "";

    HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();

    actionMode = (String)request.getAttribute(GlobalConstant.ACTION_MODE);

      if (GlobalConstant.NAV_MODE_NEW.equals(actionMode)) {
        // Create New Record Action
        this.textOnly = false;
      } else if (GlobalConstant.NAV_MODE_CHANGE.equals(actionMode)) {
        // Change Record Action
        this.textOnly = false;
      } else if (GlobalConstant.NAV_MODE_EDIT.equals(actionMode)) {
        // Edit Record Action
        this.textOnly = false;
      } else if (GlobalConstant.NAV_MODE_VIEW.equals(actionMode)) {
        // View Record Action
        this.textOnly = true;
      }
  }

  public void release() {
    super.release();
    traceChange = false;
    disabled = false;
    textOnly = false;
  }
}
